package https

import (
	"fmt"
	"log"
	"net/http"
	"time"
)

func flush(w http.ResponseWriter) {
	f, ok := w.(http.Flusher)
	if ok && f != nil {
		log.Println("flush")
		f.Flush()
	}
}

//实时刷新数据
func handler(res http.ResponseWriter, req *http.Request) {
	for i := 0; i < 10; i++ {
		fmt.Fprintf(res, "sending %d line of data\n", i)
		if f, ok := res.(http.Flusher); ok {
			f.Flush()
		} else {
			log.Println("Damn, no flush")
		}
		time.Sleep(1 * time.Second) //not real code
	}
	fmt.Fprintf(res, "sending last line of data")
}

func HttpTest() {
	http.HandleFunc("/test", handler)
	s := &http.Server{
		Addr:           ":8080",
		MaxHeaderBytes: 1 << 30,
	}

	log.Println("listening:8080")
	log.Fatalln(s.ListenAndServe())
	//	log.Println("listening:8080")
	//	http.ListenAndServe(":8080", nil)
	//	fmt.Println("启动端口:8080")
}
